import { Address, toNano } from '@ton/core';
import { BulkAdder } from '../wrappers/BulkAdder';
import { Countee } from '../wrappers/Countee';
import { NetworkProvider, sleep } from '@ton/blueprint';

const countee_address = Address.parse('EQDGQ9V-Hv-gcudNBe52Bq1uuJDmfg9McNZ-tzmWLmvC0u-s');
const bulk_address = Address.parse('EQDbBBNHlAHuzDZxkRhwfZtmvVlQ0ApOq4skmJ1A5Hodyjqr');

export async function run(provider: NetworkProvider, args: string[]) {
    // await getValue(provider);
    // await increment(provider);
    await reach(provider);
}

async function getValue(provider: NetworkProvider){
    const ui = provider.ui();

    if (!(await provider.isContractDeployed(countee_address))) {
        ui.write(`Error: Contract at address ${countee_address} is not deployed!`);
        return;
    }

    const countee = provider.open(Countee.fromAddress(countee_address));

    let val = await countee.getValue();
    console.log("countee val: " + val)
}

async function increment(provider: NetworkProvider){
    const ui = provider.ui();

    if (!(await provider.isContractDeployed(countee_address))) {
        ui.write(`Error: Contract at address ${countee_address} is not deployed!`);
        return;
    }

    const countee = provider.open(Countee.fromAddress(countee_address));

    await countee.send(
        provider.sender(),
        {
            value: toNano('0.05'),
        },
        "increment",
    );
}

async function reach(provider: NetworkProvider){
    const ui = provider.ui();

    if (!(await provider.isContractDeployed(countee_address))) {
        ui.write(`Error: Contract at address ${countee_address} is not deployed!`);
        return;
    }
    if (!(await provider.isContractDeployed(bulk_address))) {
        ui.write(`Error: Contract at address ${bulk_address} is not deployed!`);
        return;
    }

    const countee = provider.open(Countee.fromAddress(countee_address));
    let bulk_before = await countee.getValue();
    console.log("countee val before bulk added: " + bulk_before);

    const bulk = provider.open(BulkAdder.fromAddress(bulk_address));

    await bulk.send(
        provider.sender(),
        {
            value: toNano('0.05'),
        },
        {
            $$type: 'Reach',
            counter: countee_address,
            target: 1n,
        }
    );
}